/**
 * jims
 */
package com.jims.api.v1.controller.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.data.Message;
import com.jims.master.common.utils.SecurityUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.sys.entity.OrgDict;
import com.jims.master.modules.sys.service.OrgDictService;

import java.util.List;

/**
 * Controller
 * @author WangYQ
 * @version 2018-03-30
 */
@RestController
@RequestMapping(value = "orgDict")
public class OrgDictController extends BaseController {

	@Autowired
	private OrgDictService orgDictService;
	
	@ModelAttribute
	public OrgDict get(@RequestParam(required=false) String id) {
		OrgDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = orgDictService.get(id);
		}
		if (entity == null){
			entity = new OrgDict();
		}
		return entity;
	}

	@RequestMapping(value = "V100/orgDict",method ={RequestMethod.GET,RequestMethod.POST})
	public String getorgDictV100(String orgId){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
		OrgDict orgDict = orgDictService.get(orgId);
		String address = orgDict.getInputCode();
		Message mes = new Message();
		mes.setData(address);
		return SecurityUtil.encryptMsgData(mes);
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OrgDict orgDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OrgDict> page = orgDictService.findPage(new Page<OrgDict>(request, response), orgDict); 
		model.addAttribute("page", page);
		return "modules/sys/orgDictIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(OrgDict orgDict, Model model) {
		model.addAttribute("orgDict", orgDict);
		return "modules/sys/orgDictForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OrgDict orgDict, Model model) {
	    StringData data=new StringData();
		int i=orgDictService.save(orgDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OrgDict orgDict) {
	    StringData data=new StringData();
		int i=orgDictService.delete(orgDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
	/**
	 * @Title: mobileBillingV100
	 * @Description: (查询所有可签约的机构)
	 * @param orgDict
	 * @author WangYQ
	 * @date 2018/03/30
	 * @throws
	 */
	@RequestMapping(value = "V100/findAllList",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String findAllList(OrgDict orgDict) {
		Message msg=new Message();
		List<OrgDict> list=orgDictService.findAllList(orgDict);
		if(list!=null&&list.size()>0){
			msg.setStatus("1");
			msg.setData(list);
		}else{
			msg.setData("没有可签约的医院");
		}
		return SecurityUtil.encryptMsgData(msg);
	}
}